package com.test.nnooolwdeaer.dialog_wooden;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.CoteBankAccountList_wooden;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DeleteBankAccountDialog_Cote_wooden extends BottomSheetDialog {

    private Context mContext_wooden;

    private TextView accountTv_wooden;
    private ImageView accountTypeIv_wooden;
    private TextView accountTypeTv_wooden;

    private final CoteBankAccountList_wooden.CoteBankAccount bankAccount_wooden;

    public DeleteBankAccountDialog_Cote_wooden(@NonNull Context context, CoteBankAccountList_wooden.CoteBankAccount bankAccount) {
        super(context,R.style.BottomSheetDialogStyle);
        mContext_wooden = context;
        this.bankAccount_wooden = bankAccount;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_delete_bank_account_cote_wooden);
        getWindow().setBackgroundDrawableResource(R.color.trans);
        setCancelable(false);
        init_wooden();
    }

    @SuppressLint("SetTextI18n")
    private void init_wooden(){

        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        accountTv_wooden = findViewById(R.id.dialog_delete_bank_account_tv_account);
        accountTypeIv_wooden = findViewById(R.id.dialog_delete_bank_account_iv_wallet_type);
        accountTypeTv_wooden = findViewById(R.id.dialog_delete_bank_account_tv_wallet_type);

        findViewById(R.id.dialog_delete_bank_account_btn_cancel).setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("DELETE_ACCOUNT_CANCEL_CLICK");
            dismiss();
        });

        if (bankAccount_wooden ==null) return;

        accountTv_wooden.setText(bankAccount_wooden.getWalletMobile());
        accountTypeTv_wooden.setText(bankAccount_wooden.getWalletDesc());

        //获取图标
        if (bankAccount_wooden.getBitmap()!=null){
            accountTypeIv_wooden.setImageBitmap(bankAccount_wooden.getBitmap());
        }else {
            ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(() -> {
                try {
                    URL url = new URL(bankAccount_wooden.getWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        bankAccount_wooden.setBitmap(bitmap);
                        ThreadUtil_wooden.getInstance_wooden().runOnUiThread_wooden(() -> accountTypeIv_wooden.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    if (Constant_wooden.Debug) e.printStackTrace();
                }
            });
        }

        findViewById(R.id.dialog_delete_bank_account_btn_delete).setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("DELETE_ACCOUNT_SUBMIT_CLICK");
            HttpManager_wooden.getInstance_wooden().deleteBoundBankAccount_wooden(bankAccount_wooden.getId());
            dismiss();
        });

    }

    @Override
    public void show() {
        super.show();
        HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("DELETE_ACCOUNT_DIALOG_COTE_START");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("DELETE_ACCOUNT_DIALOG_COTE_END");
    }
}
